package com.glwan.fixedassetapp.utils;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ConditionWrapper {

    public final static String AND = "AND";
    public final static String EQ = "=";
    public final static String NE = "<>";
    public final static String GE = ">=";
    public final static String GT = ">";
    public final static String LE = "<=";
    public final static String LT = "<";
    public final static String LIKE = "LIKE";
    public final static String OR = "OR";
    public final static String IN = "IN";
    public final static String NOT = "NOT";
    public final static String SPACE = " ";
    public final static String IS = "IS";
    public final static String NULL = "NULL";
    public final static String NOT_IN = NOT + SPACE + IN;
    public final static String NOT_LIKE = NOT + SPACE + LIKE;
    public final static String IS_NULL = IS + SPACE + NULL;
    public final static String IS_NOT_NULL = IS + SPACE + NOT + SPACE + NULL;
    public final static String GROUP_BY = "GROUP_BY";
    public final static String HAVING = "HAVING";
    public final static String ORDER_BY = "ORDER_BY";
    public final static String EXISTS = "EXISTS";
    public final static String NOT_EXISTS = NOT + SPACE + EXISTS;
    public final static String BETWEEN = "BETWEEN";
    public final static String NOT_BETWEEN = NOT + SPACE + BETWEEN;
    public final static String ASC = "ASC";
    public final static String DESC = "DESC";
    public final static String PERCENT = "%";
    public final static String QUESTION = "?";
    public final static String LEFT_PARENTHESIS = "(";
    public final static String RIGHT_PARENTHESIS = ")";
    private String conditionClause = "";
    private List<String> conditionArgs = new ArrayList<>();

    /**
     * 拼接eq语句
     *
     * @param columnName 字段名
     * @param value      字段的值
     */
    public void eq(String columnName, Object value) {
        formatSql(columnName + SPACE + EQ + SPACE, value);
    }

    /**
     * 拼接ne语句
     *
     * @param columnName 字段名
     * @param value      字段的值
     */
    public void ne(String columnName, Object value) {
        formatSql(columnName + SPACE + NE + SPACE, value);
    }

    /**
     * 拼接左like语句
     *
     * @param columnName 字段名
     * @param value      字段的值
     */
    public void leftLike(String columnName, Object value) {
        formatSql(columnName + LIKE + SPACE, PERCENT + value);
    }

    /**
     * 拼接右like语句
     *
     * @param columnName 字段名
     * @param value      字段的值
     */
    public void rightLike(String columnName, Object value) {
        formatSql(columnName + LIKE + SPACE, value + PERCENT);
    }

    /**
     * 拼接like语句
     *
     * @param columnName 字段名
     * @param value      字段的值
     */
    public void like(String columnName, Object value) {
        formatSql(columnName + SPACE + LIKE + SPACE, PERCENT + value + PERCENT);
    }

    /**
     * 拼接in语句
     *
     * @param columnName 字段名
     * @param valueArray 字段的值
     */
    public void in(String columnName, String[] valueArray) {
        StringBuilder sb = new StringBuilder();
        sb.append(columnName).append(SPACE).append(IN).append(SPACE).append(LEFT_PARENTHESIS);
        for (int i = 0; i < valueArray.length; i++) {
            sb.append("?");
            if (i < valueArray.length - 1) {
                sb.append(",");
            }
        }
        sb.append(SPACE).append(RIGHT_PARENTHESIS);
        formatInSql(sb.toString(), valueArray);
    }

    /**
     * 格式化in语句
     *
     * @param condition  条件
     * @param valueArray 值
     */
    private void formatInSql(String condition, String[] valueArray) {
        if (StringUtils.isNotEmpty(conditionClause)) {
            condition = SPACE + AND + SPACE + condition;
        }
        conditionClause += condition;
        conditionArgs.addAll(Arrays.asList(valueArray));
    }

    /**
     * 格式化sql
     *
     * @param condition 条件
     * @param value     值
     */
    private void formatSql(String condition, Object value) {
        if (StringUtils.isNotEmpty(conditionClause)) {
            condition = SPACE + AND + SPACE + condition;
        }
        conditionClause += SPACE + condition + SPACE + QUESTION;
        conditionArgs.add(String.valueOf(value));
    }

    public List<String> getConditionArgs() {
        return this.conditionArgs;
    }

    public String getConditionClause() {
        return this.conditionClause;
    }

    public void resetCondition() {
        this.conditionArgs.clear();
        this.conditionClause = "";
    }
}
